<script>
import * as types from '../store/types';
export default {
  name: 'WorkSpaceQuery',
  data() {
    return {};
  },
  beforMount() {
    this.$el.remove();
  },
  methods: {
    // 新增一个空应用
    addNewApp(appExEntity, infoEntity) {
      return new Promise((resolve, reject) => {
        const newApp = api.datav.addApp({
          appExEntity,
          infoEntity,
        });
        resolve(newApp);
      });
    },
    // 获取所有的应用列表
    getAllAppList(options) {
      return new Promise((resolve, reject) => {
        const getAllAppList = api.datav.findAllAppList(options);
        resolve(getAllAppList);
      });
    },
    // 获取应用列表
    getAppList(objCondition, pageIndex, pageSize) {
      return new Promise((resolve, reject) => {
        const getAppList = api.datav.findAppList({
          objCondition,
          orderby: 'createTime_desc', // 排序（选填）
          pageIndex, // 当前页索引（必填）
          pageSize, // 每页显示的条数（必填）
        });
        resolve(getAppList);
      });
    },
    // 工作区获取应用列表后的操作
    setWorkSpaceAppList(response) {
      // 当没有应用时，提示跳转到app页面新增应用。
      if (response.data.dataSource.length === 0) {
        this.$events.emit('on-showSpin', false);
        this.$Modal.info({
          title: '提示',
          content: '现没有可编辑的应用，请前往新增一个应用！',
          onOk: () => {
            this.$router.push('/app');
          },
        });
      } else {
        // 当前缓存的应用不存在时，默认显示最近生成的应用
        this.appName = response.data.dataSource[0].name;
        this.id = response.data.dataSource[0].id;
        this.$route.params.id = this.id;
        const dataInfo = this.$store.dispatch(types.GET_APP_DATA, this.id);
        this.$store.commit(types.UPDATE_CHART_CONFIG, { data: dataInfo, isAllUpdata: true });
        this.$router.push('/workspace/' + this.id);
        this.$events.emit('on-showSpin', false);
      }
    },
    // 工作区做读取应用的操作
    async getLastAppData() {
      this.$events.emit('on-showSpin', true);
      if (Object.keys(this.$store.state.app.appData).length !== 0) {
        // this.appName = this.$store.state.app.appData.name;
        this.id = this.$store.state.app.appData.id;
        // 请求数据
        new Promise((resolve, reject) => {
          const hasList = this.$store.dispatch(types.GET_APP_DATA, this.id);
          resolve(hasList);
        }).then(async res => {
          if (!res) {
            const response = await this.getAppList(
              {
                typeId: '40031',
              },
              1,
              1
            );
            this.setWorkSpaceAppList(response);
          } else {
            this.$events.emit('on-showSpin', false);
            this.setAppName = res.name;
            this.appName = res.name;
            this.setAppDescription = res.description;
            // this.$store.commit(types.UPDATE_APPLY_CONFIG, { data: res.info, isAllUpdata: true });
          }
        });
      } else {
        const response = await this.getAppList(
          {
            typeId: '40031',
          },
          1,
          1
        );
        this.setWorkSpaceAppList(response);
      }
    },
    // 更新应用
    async upDataAppConfig(appExEntity, infoEntity) {
      const response = await api.datav.updateApp({
        appExEntity,
        infoEntity,
      });
      return response;
    },
    // 修改名称,描述配置的请求
    isUpDataApp(name, description) {
      new Promise((resolve, reject) => {
        const response = this.upDataAppConfig(
          {},
          {
            id: this.id,
            alias: name,
            name,
            description,
          }
        );
        resolve(response);
      }).then(res => {
        this.$Message.success('修改成功！');
        if (res.data) {
          this.$store.commit(types.UPDATA_APP_DATA, {
            id: this.id,
            name: this.appName,
          });
          this.appName = this.setAppName;
        }
      });
    },
    // 删除应用
    async deleteApp(id) {
      const response = await api.datav.deleteApp({ id });
    },
  },
};
</script>

<template>
  <div> </div>
</template>

<style>
</style>
